package com.two.service.restaurant.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.two.common.entity.Restaurant;
import com.two.common.util.CommonResult;
import com.two.common.vo.RestaurantVo;
import com.two.service.restaurant.mapper.RestaurantMapper;
import com.two.service.restaurant.service.RestaurantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class RestaurantServiceImpl implements RestaurantService {

    @Autowired
    private RestaurantMapper restaurantMapper;

    @Override
    public CommonResult selectAllByCondition(Integer currentPage, Integer pageSize, RestaurantVo restaurantVo) {
        Page<Restaurant> page = new Page<>(currentPage,pageSize);
        QueryWrapper wrapper = new QueryWrapper();
        if (StringUtils.hasText(restaurantVo.getRestaurantName())) {
            wrapper.like("restaurantname", restaurantVo.getRestaurantName());
        }
        restaurantMapper.selectPage(page,wrapper);
        return new CommonResult(2000,"查询成功",page);
    }

    @Override
    public Integer addRestaurant(RestaurantVo restaurantVo) {
        Restaurant restaurant = new Restaurant(null,restaurantVo.getRestaurantName(), restaurantVo.getAddress(), restaurantVo.getPhone(), restaurantVo.getRemark(),restaurantVo.getUrl(), null,null,null);
        int row = restaurantMapper.insert(restaurant);
        Integer rid = restaurant.getRid();
        if (row==1){
            return rid;
        }
        return 0;
    }

    @Override
    public CommonResult updateRestaurant(RestaurantVo restaurantVo) {
        Restaurant restaurant = new Restaurant(restaurantVo.getRid(),restaurantVo.getRestaurantName(), restaurantVo.getAddress(), restaurantVo.getPhone(), restaurantVo.getRemark(),restaurantVo.getUrl(), null,null,null);
        int row = restaurantMapper.updateById(restaurant);
        if (row==1){
            return new CommonResult(2000,"修改成功",null);
        }
        return new CommonResult(5000,"修改失败,请联系管理员",null);
    }

    @Override
    public CommonResult deleteRestaurant(RestaurantVo restaurantVo) {
        Integer rid = restaurantVo.getRid();
        int row = restaurantMapper.deleteById(rid);
        if (row==1){
            return new CommonResult(2000,"删除成功",null);
        }
        return new CommonResult(5000,"删除失败,请联系管理员",null);
    }

    @Override
    public CommonResult selectAll() {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("is_deleted",0);
        List list = restaurantMapper.selectList(wrapper);
        return new CommonResult(2000,"查询成功",list);
    }

    @Override
    public CommonResult selectByRid(Integer rid) {
        Restaurant restaurant = restaurantMapper.selectById(rid);
        if (restaurant!=null){
            return new CommonResult(2000,"查询成功",restaurant);
        }
        return new CommonResult(5000,"查询失败,请联系管理员",null);
    }

    @Override
    public CommonResult selectAllByRestaurantName(String restaurantName) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("is_deleted",0);
        if (StringUtils.hasText(restaurantName)){
            wrapper.like("restaurantname", restaurantName);
        }
        List list = restaurantMapper.selectList(wrapper);
        return new CommonResult(2000,"查询成功",list);
    }
}
